package com.tarena.lbs.content.service.impl;

import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategory;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import com.tarena.lbs.content.utils.PageResultBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    private final CategoryRepository categoryRepository;
    @Override
    public void saveArticleCategory(ArticleCategoryParam param) {
        ArticleCategory category = new ArticleCategory();
        BeanUtils.copyProperties(param,category);
        if (category.getId() == null) {
            category.setCreateTime(new Date());
            category.setUpdateTime(new Date());
            categoryRepository.addArticleCategory(category);
        }else {
            category.setUpdateTime(new Date());
            categoryRepository.updateArticleCategory(category);
        }
    }
    @Override
    public PageResult<ArticleCategoryVO> listAllArticleCategory() {
        List<ArticleCategoryVO> defaultList = categoryRepository.getAllArticleCategory();
        return PageResultBuilder.build(defaultList);
    }
}
